package com.test;

import ij.process.ColorProcessor;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

import com.iamk.feature.FeaturesColor;
import com.iamk.util.CreateImageRegion;
import com.iamk.util.ListFilesUtil;

import de.lmu.ifi.dbs.jfeaturelib.features.Tamura;

public class CreateDataNonWavelet {
	public static void main(String[] args) {
		NumberFormat nf = new DecimalFormat("#.######");
		BufferedWriter bw = null;
		ListFilesUtil lfu = new ListFilesUtil();
		lfu.listAllFiles("C:\\Users\\Ken\\Desktop\\data-mcr\\Images\\");
		CreateImageRegion cir;
		try {
			bw = new BufferedWriter(new FileWriter("C:\\Users\\Ken\\Desktop\\data-mcr\\features\\NonWavelet\\features-tamura-dcd.arff",true));
			// Writer header file arff
			bw.write("@RELATION feature-tamura-dcd\n");
			for (int i = 0; i < 21; i++) {
				bw.write("@ATTRIBUTE f" + (i + 1) + "	REAL\n");
			}
			bw.write("@attribute class {");
			for(int i = 0; i < 21; i++){
				int temp = i;
				temp++;
				if(i == 20){
					bw.write(temp + "");
				}else{
					bw.write(temp + ",");
				}
				
			}
			bw.write("}\n");
			bw.write("@data\n");
			
			for(int f = 0; f < lfu.arrAllFile.size();f++){
				System.out.println(lfu.arrFileName.get(f));
				String filename = lfu.arrFileName.get(f).replace(".jpg", "");
				filename = filename.replace(".bmp", "");
				cir = new CreateImageRegion(lfu.arrAllFile.get(f), "C:\\Users\\Ken\\Desktop\\data-mcr\\Mask\\"+filename+".txt");
				cir.loadData(cir.imgPath, cir.maskPath);
				
				for(int i = 0; i < cir.listRegion.size(); i++){
					Tamura mDescriptorApp = new Tamura();
			        mDescriptorApp.run(new ColorProcessor(cir.listRegion.get(i)));
					List<double[]> featuresTamura = mDescriptorApp.getFeatures();
					for (double[] feature : featuresTamura) {
						for (int j = 0; j < feature.length; j++) {
							bw.write(nf.format(feature[j]) + ", ");
						}
					}
			        
			        FeaturesColor mColorDescriptor = new FeaturesColor(cir.listRegion.get(i));
			        List<Double> featureColor = mColorDescriptor.getFeatures();
					for (int j = 0; j < featureColor.size(); j++) {
						bw.write(nf.format(featureColor.get(j)) + ", ");
					}
					bw.write(cir.listLabel.get(i) + "\n");
				}
			}
			bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
